package modelTest.threadBasedModel;

import java.awt.Graphics;
import java.awt.image.BufferedImage;

import modelTest.model.Coordinate;
import modelTest.model.Moveable;
import modelTest.model.mobs.MobParam;
import modelTest.view.view2D.Assets;
import modelTest.view.view2D.Drawable;

public class MobThread2D extends Thread implements Drawable, Moveable {

	public static final int COLLISION_RAY = 80;
	
	private long mobID;
	private Coordinate coo;
	private int shiftAmount;
	private BufferedImage mobSprite;
	private boolean isRunning = false;
	
	public MobThread2D(long mobID, Coordinate mobCoordinate, int shiftAmount) {
		this.mobID = mobID;
		this.coo = mobCoordinate;
		this.shiftAmount = shiftAmount;
		mobSprite = Assets.getsLoader().getAsset(Assets.MOB_2D);
	}
	
	@Override
	public void run() {
		isRunning = true;
		boolean isKillable = false;
		while(!isKillable){
			if(isRunning){
				isKillable = calculateNewCoordinate();
				if (isKillable) {
					System.out.println("ID "+ mobID+" say: kill me please");
				}
			}
			try {
				sleep(MobParam.SLEEP_MILLIS);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public void setRunning( boolean isRunning ){
		this.isRunning = isRunning;
	}
	
	public int getShiftAmount() {
		return shiftAmount;
	}

	@Override
	public boolean calculateNewCoordinate() {
		int newY = coo.getY()+ shiftAmount;
		
		if( newY >= Coordinate.MOB_2D_MAX_Y ){
			return true;
		}
		coo.setY(newY);
		return false;
	}
	
	@Override
	public void draw(Graphics g) {
		g.drawImage(mobSprite, coo.getX(), coo.getY(), null);
	}

}
